package com.kin207.netty.config;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.ConstructorUtils;

import com.kin207.netty.listener.Listener;

public class CListener {
	private String name;
	private String className;
	private String type;
	private Map<String,String> initConfig;
	private String scope;
	
	private Listener single;
	
	public Listener getListener() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException{
		Listener result = null;
		if(scope==null || scope.trim().equals("") || "single".equals(scope)){
			if(single==null){
				Class<Listener> clazz = (Class<Listener>) Class.forName(className);
				single = ConstructorUtils.invokeConstructor(clazz, new Object[0]);
			}
			result = single;
		}else{
			Class<Listener> clazz = (Class<Listener>) Class.forName(className);
			result = ConstructorUtils.invokeConstructor(clazz, new Object[0]);
		}
		return result;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Map<String, String> getInitConfig() {
		return initConfig;
	}
	public void setInitConfig(Map<String, String> initConfig) {
		this.initConfig = initConfig;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}
	public Listener getSingle() {
		return single;
	}

	public void setSingle(Listener single) {
		this.single = single;
	}
	
}
